﻿Public Class FormCategoryEditor
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        Dim dt As DataTable = Cmd.ExecuteDataTable("SELECT * FROM Type WHERE Active=1")
        Me.cboType.DisplayMember = "TypeName"
        Me.cboType.ValueMember = "TypeId"
        Me.cboType.DataSource = dt

    End Sub

    Private Sub FormCategoryEditor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click

        If Me.txtCategoryName.Text = "" Then
            MsgBox("Please input Category name!")
            Me.txtCategoryName.Focus()
            Return
        End If

        Cmd.Parameters("@CategoryId") = txtCategoryId.Text
        Cmd.Parameters("@CategoryName") = txtCategoryName.Text
        Cmd.Parameters("@TypeId") = cboType.SelectedValue
        If Me.txtCategoryId.Text = "" Then
            Cmd.ExecuteNonQuery("INSERT INTO Category VALUES(@CategoryName,@TypeId,1);")
            MsgBox("Ok")
        Else
            Cmd.Parameters("@CategoryId") = txtCategoryId.Text
            Cmd.ExecuteNonQuery("UPDATE Category SET CategoryName=@CategoryName,TypeId=@TypeId WHERE CategoryId=@CategoryId")
        End If

        Me.DialogResult = Windows.Forms.DialogResult.OK

    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click

        Me.DialogResult = Windows.Forms.DialogResult.Cancel

    End Sub
End Class